//
//  CYYNetWorkManager+PartyParticipatorsList.m
//  LetsPartyBmob
//
//  Created by rimi on 15/9/10.
//  Copyright (c) 2015年 柴阳阳. All rights reserved.
//

#import "CYYNetWorkManager+PartyParticipatorsList.h"

@implementation CYYNetWorkManager (PartyParticipatorsList)
//活动参加者列表
/*
 partyObjectId:活动id  个界面传值过来的
 
 */
+ (void)partyParticipatorsListWithPartyObjectId:(NSString *)partyObjectId
                                         sucess:(SucessPartyParticipatorsList)sucess
                                           fail:(Fail)fail{
    //关联对象表
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"_User"];
    
    //需要查询的列
    BmobObject * party = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
    [bquery whereObjectKey:@"partyParticipators" relatedTo:party];
    //降序
    [bquery orderByDescending:@"createdAt"];
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            fail(error);
        } else {
            sucess(array);
        }
    }];
}

////获得活动参加的人数
//+ (void)partyParticipatorsNumberWithPartyObjectId:(NSString *)partyObjectId
//                                           sucess:(SucessPartyParticipatorsListNumber)sucessNumber
//                                             fail:(Fail)fail{
//    [CYYNetWorkManager partyParticipatorsListWithPartyObjectId:partyObjectId sucess:^(NSArray *array) {
//        sucessNumber(array.count);
//    } fail:^(NSError *error) {
//        fail(error);
//    }];
//}

//获得活动参加的人数
+ (void)partyParticipatorsNumberWithPartyObjectId:(NSString *)partyObjectId
                                           sucess:(SucessPartyParticipatorsListNumber)sucessNumber
                                             fail:(Fail)fail{
    //关联对象表
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"_User"];
    
    //需要查询的列
    BmobObject * party = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
    [bquery whereObjectKey:@"partyParticipators" relatedTo:party];
    //降序
    [bquery orderByDescending:@"createdAt"];
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            fail(error);
        } else {
            sucessNumber(array.count);
        }
    }];
}

//-----
//活动参加者列表
/*
 partyObjectId:活动id  个界面传值过来的
 
 */

+ (void)newPartyParticipatorsListWithPartyObjectId:(NSString *)partyObjectId
                                         sucess:(SucessPartyParticipatorsListArray)sucess
                                           fail:(Fail)fail{
    NSMutableArray * observerArray = [NSMutableArray array];
    //关联对象表
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"_User"];
    
    //需要查询的列
    BmobObject * party = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
    [bquery whereObjectKey:@"partyParticipators" relatedTo:party];
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            fail(error);
        } else {
            for (int i = 0; i < array.count; i ++) {
                NSDictionary * userInfoDict = @{@"objectId":((BmobObject *)array[i]).objectId,
                                                @"mobilePhoneNumber":[array[i] objectForKey:@"mobilePhoneNumber"],
                                                @"name":[array[i] objectForKey:@"name"]};
                [observerArray addObject:userInfoDict];
            }
            sucess(observerArray);
        }
    }];
}


@end
